package com.example.take_out_admin.service.impl;



import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.exception.ResponseEnum;
import com.example.take_out_admin.exception.business.AuthenticationException;
import com.example.take_out_admin.mapper.EmployeeMapper;
import com.example.take_out_admin.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.take_out_admin.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-06-27
 */
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;
    @Autowired
    private  JWTUtil jwtUtil;
    @Autowired
    private HttpServletRequest httpServletRequest;


    @Override
    public String login(Employee employee) {

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //SELECT * FROM employee WHERE username = 'some_username';--employee.getUsername()
        wrapper.eq(Employee::getUsername,employee.getUsername());
        //通过这个条件获得一个Employee对象
        Employee one = this.getOne(wrapper);

        //log.info("{}",one);
        if(one==null) {
            throw new AuthenticationException(ResponseEnum.INTERNAL_SERVER_ERROR_USERNOTFOUND);
        }
        //密码明文->密码密文->比对
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if(!one.getPassword().equals(password)){
            throw new AuthenticationException(ResponseEnum.INTERNAL_SERVER_ERROR_USERNOTFOUND);
        }

        String token = jwtUtil.generateToken(one.getId(),one.getUsername());

//        return Result.success(ResponseEnum.SUCCESS,one);  返回成功及该员工具体信息

        return token;
    }

    @Override
    public Page<Employee> getEmployeePage(int page, int pageSize, String name) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null&&name!="",Employee::getName, name);
        Page<Employee> employeePage = this.page(new Page<>(page, pageSize), wrapper);
        return employeePage;

    }

    @Override
    public void saveEmployee(Employee employee) {
        String updateUser = getUpdateUserFromToken();

        employee.setCreateUser(updateUser);
        employee.setUpdateUser(updateUser);

        this.save(employee);

    }

    @Override
    public void updateEmployee(Employee employee) {
        String updateUser = getUpdateUserFromToken();
        employee.setUpdateUser(updateUser);

        this.updateById(employee);
    }
    private String getUpdateUserFromToken() {
        String token = httpServletRequest.getHeader("Authentication");
        if(jwtUtil.isTokenExpired(token)){
            throw new AuthenticationException(ResponseEnum.TOKEN_TIME_OUT);
        }
        String updateUse = jwtUtil.extractUsername(token);

        return updateUse;
    }
    private Claims getClaimsFromToken() {
        String token = httpServletRequest.getHeader("Authentication");
        if(jwtUtil.isTokenExpired(token)){
            throw new AuthenticationException(ResponseEnum.TOKEN_TIME_OUT);
        }
//        String updateUse = jwtUtil.extractUsername(token);
        Claims claimsResult = jwtUtil.extractClaim(token, claims -> claims);

        return claimsResult;
    }


}
